package com.hwz.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hwz.annotation.AutoFill;
import com.hwz.constant.MessageConstant;
import com.hwz.constant.StatusConstant;
import com.hwz.dto.CategoryDTO;
import com.hwz.dto.CategoryPageQueryDTO;
import com.hwz.entity.Category;
import com.hwz.exception.DeletionNotAllowedException;
import com.hwz.mapper.CategoryMapper;
import com.hwz.mapper.DishMapper;
import com.hwz.mapper.SetmealMapper;
import com.hwz.result.PageResult;
import com.hwz.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * [一句话描述该类的功能]
 *
 * @author : [Lenovo]
 * @version : [v1.0]
 * @createTime : [2024/3/13 21:16]
 */
@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    CategoryMapper categoryMapper;
    @Autowired
    DishMapper dishMapper;
    @Autowired
    SetmealMapper setmealMapper;

    /**
     * 查询所有分类
     * @param categoryPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageHelper.startPage(categoryPageQueryDTO.getPage(),categoryPageQueryDTO.getPageSize());
        Page<Category> page= categoryMapper.page(categoryPageQueryDTO.getName(),categoryPageQueryDTO.getType());
        List<Category> categories = page.getResult();
        return new PageResult(page.getTotal(),categories);
    }

    /**
     * 更新分类
     * @param categoryDTO
     */
    @Override
    public void updateCategory(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        categoryMapper.update(category);
    }

    /**
     * 添加分类
     * @param categoryDTO
     */
    @Override
    public void addCategory(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        category.setStatus(StatusConstant.ENABLE);//默认启用
        categoryMapper.insert(category);
    }

    /**
     * 修改分类状态
     * @param id
     * @param status
     */
    @Override
    public void openOrOffCategory(Long id, Integer status) {
        Category category = Category.builder().id(id).status(status).build();
        categoryMapper.update(category);
    }


    /**
     * 根据类型查询分类
     * @param type
     * @return
     */
    @Override
    public List<Category> getByType(Integer type) {
        List<Category> categories = categoryMapper.getByType(type);
        return categories;
    }

    /**
     * 删除分类
     * @param id
     */
    @Override
    public void deleteCategory(Long id) {
        //查询当前分类是否关联了菜品，如果关联了就抛出业务异常
        Integer dishCount = dishMapper.getCountByCategoryId(id);
        if (dishCount > 0)
        {
            //当前分类下有菜品，不能删除
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }
        //查询当前分类是否关联了套餐，如果关联了就抛出业务异常
        Integer setmealCount = setmealMapper.getCountByCategoryId(id);
        if (setmealCount > 0)
        {
            //当前分类下有套餐，不能删除
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }
        categoryMapper.delete(id);
    }

    @Override
    public List<Category> list(Integer type) {
        List<Category> categories = categoryMapper.getByType(type);
        return categories;
    }


}
